'''
Created on 19/05/2009

@author: eh01
'''
from yarra.datastore import dsofactory

class UnitOfWork(object):
    '''
    classdocs
    '''
    
    dsos = None
    __snapshot_of_dsos = None
    
    def __init__(self, snapshot_of_dsos):
        '''
        Constructor
        '''
        self.__snapshot_of_dsos = {}
        self.__dsos = []
        
        # create a copy of the dsos
        for snapshot_dso in snapshot_of_dsos:
            copy_of_dso = dsofactory.copy_dso(snapshot_dso)
            self.__dsos.append(copy_of_dso)
            self.__snapshot_of_dsos[snapshot_dso.id] = snapshot_dso
        
    def get_updated_dsos(self):
        updated_dsos = []
        for dso in self.__dsos:
            if dso.is_new():
                continue
            snapshot_dso = self.__snapshot_of_dsos[dso.id]
            if dso != snapshot_dso:
                updated_dsos.append(dso)
        return updated_dsos

    def get_new_dsos(self):
        return [dso for dso in self.__dsos if dso.is_new()]